import { pinus } from 'pinus';
import { preload } from './preload';

import _pinus = require('pinus');
import { GlobalConfig } from './config/global/global_config';
import route_util from './app/utils/route_util';
import { FightService } from './app/services/fight_service';
import  ValidatePlayerFilter  from './app/filters/validate_player';
import validate_player from './app/filters/validate_player';
import error_handler from './app/filters/error_handler';
import { PushService } from './app/services/push_service';
import { EquipmService } from './app/services/equipm_service';

const filePath = (_pinus as any).FILEPATH;
filePath.MASTER = '/config/master';
filePath.SERVER = '/config/servers';
filePath.CRON = '/config/crons';
filePath.LOG = '/config/log4js';
filePath.SERVER_PROTOS = '/config/serverProtos';
filePath.CLIENT_PROTOS = '/config/clientProtos';
filePath.MASTER_HA = '/config/masterha';
filePath.LIFECYCLE = '/lifecycle';
filePath.SERVER_DIR = '/app/servers/';
filePath.CONFIG_DIR = '/config';

const adminfilePath = _pinus.DEFAULT_ADMIN_PATH;
adminfilePath.ADMIN_FILENAME = 'adminUser';
adminfilePath.ADMIN_USER = 'config/adminUser';

GlobalConfig.init();

/**
 *  替换全局Promise
 *  自动解析sourcemap
 *  捕获全局错误
 */
preload();

/**
 * Init app for client.
 */
let app = pinus.createApp();
app.set('name', 'game-server');

// app configuration
app.configure('production|development', 'connector', function () {
    app.set('connectorConfig',
        {
            connector: pinus.connectors.hybridconnector,
            // heartbeat: 3,
            timeout: 30,
            useDict: true,
            useProtobuf: true
        });
});

app.configure('production|development', 'gate', function () {
    app.set('connectorConfig',
        {
            connector: pinus.connectors.hybridconnector,
            useProtobuf: true
        });
});

app.configure('production|development', 'fight',function () {
    // route configures
    app.route('fight', route_util.fight);

    // filter configures
    app.filter(new pinus.filters.timeout(3000, 1000));
    app.filter(validate_player());
    app.filter(error_handler(app));
    new PushService(app);

    EquipmService.ins();
});

app.configure('development', function () {
    // enable the system monitor modules
    app.enable('systemMonitor');
});

// start app
app.start();

